package com.jxnd.sunflower.controller;

import com.jxnd.sunflower.common.JSONResult;
import com.jxnd.sunflower.domain.RUser;
import com.jxnd.sunflower.domain.bo.LoginBO;
import com.jxnd.sunflower.domain.bo.RetrievePsswordBO;
import com.jxnd.sunflower.domain.bo.UserBO;
import com.jxnd.sunflower.domain.dto.UserDTO;
import com.jxnd.sunflower.exception.SunFlowerException;
import com.jxnd.sunflower.service.UserService;
import com.jxnd.sunflower.utils.JwtToken;
import com.jxnd.sunflower.utils.LocalUser;
import com.jxnd.sunflower.utils.MobileEmailUtils;
import com.jxnd.sunflower.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: 敷衍
 * @description: 用户相关
 * @date: 2021/11/21 11:03
 * @version: 1.0
 */
@Api(value = "用户",tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;
    
    @Autowired
    private RedisOperator redis;


    @PostMapping("/Registered")
    @ApiOperation(value = "用户注册",notes = "用户注册")
    private JSONResult registered(@Valid UserBO user){
        String code = user.getCode();
        String verificationCode = redis.get(user.getEmail());
        if(!code.equals(verificationCode)){
            throw new SunFlowerException("验证码输入错误",502);
        }
        String token = service.addUser(user);
        //todo
        return new JSONResult(token);
    }

    @GetMapping("/sendCode")
    @ApiOperation(value = "发送验证码",notes = "发送验证码通用方法")
    private JSONResult sendCode(@RequestParam String email){
        if(!MobileEmailUtils.checkEmailIsOk(email)){
            throw new SunFlowerException("请输入正确的邮箱格式",503);
        }
       service.emailCode(email);

        return new JSONResult("发送成功");
    }


    @PostMapping("/RetrievePassword")
    @ApiOperation(value = "找回密码",notes = "找回密码")
    private JSONResult RetrievePassword(@Valid RetrievePsswordBO psswordBO){
        String code = psswordBO.getCode();
        String verificationCode = redis.get(psswordBO.getEmail());
        if(!code.equals(verificationCode)){
            throw new SunFlowerException("验证码输入错误",502);
        }
        int result  = service.RetrievePassword(psswordBO);

        return new JSONResult("找回密码成功");
    }



    @PostMapping("/login")
    @ApiOperation(value = "登录",notes = "登录")
    private JSONResult login(@RequestBody @Valid LoginBO bo){

        String token = service.login(bo);

        return new JSONResult(token);
    }

    @GetMapping("/userinfo")
    @ApiOperation(value = "获取用户信息",notes = "获取用户信息")
    private JSONResult userInfo(@RequestHeader("Authorization") String token){
        int userId = JwtToken.getUserId(token);

        UserDTO userDTO = service.userInfo(userId);


        if(userDTO==null){
            throw new SunFlowerException("你还没有登录");
        }
        return new JSONResult(userDTO);
    }





}
